package com.dxl.dao.account;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.dxl.common.model.Page;
import com.dxl.common.utils.SqlBuilder;
import com.dxl.common.utils.Utils;
import com.dxl.dao.common.BaseDao;
import com.dxl.entity.account.Role;

/**
 * 角色管理DAO
 * @author 邓小林
 */
@Repository
public class RoleDao extends BaseDao {
	private static final Logger LOG = Logger.getLogger(RoleDao.class);
	
	public Page<Role> list(String[] pids, String keyword, Integer state, Integer pageNo, Integer pageSize) {
		SqlBuilder sb = new SqlBuilder("select * from role where 1 = 1");
		if (Utils.isNotEmpty(pids)) {
			sb.appendStatement(" and parent_id in ").appendParamValues(pids);
		}
		if (Utils.isNotBlank(keyword)) {
			keyword = keyword.trim();
			sb.appendStatement(" and name like ").appendParamValue("%" + keyword + "%");
		}
		if (Utils.isNotNull(state)) {
			sb.appendStatement(" and state = ").appendParamValue(state);
		}
		sb.appendStatement(" order by created_time desc");
		return this.list(Role.class, sb.toStatement(), sb.toParams(), pageNo, pageSize);
	}
	
	public int deleteRoleFunction(String[] functionIds) {
		if (Utils.isEmpty(functionIds)) {
			return 0;
		}
		SqlBuilder sb = new SqlBuilder("delete from role_function where function_id in ").appendParamValues(functionIds);
		return this.del(sb.toStatement(), sb.toParams());
	}
}
